package com.njym.java.security.app;

import com.njym.java.EnableAceCache;

import com.njym.java.security.app.rest.client.Const;
import com.njym.java.security.app.rest.client.HelloClientAioHandler;
import com.njym.java.security.auth.client.EnableAceAuthClient;
import com.spring4all.swagger.EnableSwagger2Doc;

import org.jim.common.packets.ChatBody;
import org.jim.common.packets.Command;
import org.jim.common.packets.LoginReqBody;
import org.jim.common.tcp.TcpPacket;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.tio.client.AioClient;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Aio;
import org.tio.core.Node;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-05-25 12:44
 */
@EnableDiscoveryClient
@EnableCircuitBreaker
@SpringBootApplication
@EnableFeignClients({"com.njym.java.security.auth.client.feign"})
@EnableScheduling
@EnableAceAuthClient
@EnableAceCache
@EnableTransactionManagement
@MapperScan("com.njym.java.security.app.mapper")
@EnableSwagger2Doc
public class AppBootstrap {

    /**
     * /服务器节点
     */
//    public static Node serverNode = new Node("192.168.2.172", Const.SERVER_PORT);
    public static Node serverNode = new Node("59.110.7.100", Const.SERVER_PORT);
    /**
     * handler, 包括编码、解码、消息处理
     */
    public static ClientAioHandler aioClientHandler = new HelloClientAioHandler();

    /**
     * 事件监听器，可以为null，但建议自己实现该接口，可以参考showcase了解些接口
     */
    public static ClientAioListener aioListener = null;

    /**
     * 断链后自动连接的，不想自动连接请设为null
     */
    private static ReconnConf reconnConf = new ReconnConf(5000L);

    /**
     * 一组连接共用的上下文对象
     */
    public static ClientGroupContext clientGroupContext = new ClientGroupContext(aioClientHandler, aioListener, reconnConf);

    public static AioClient aioClient = null;
    public static ClientChannelContext clientChannelContext = null;

    public static void main(String[] args) throws Exception {
        clientGroupContext.setHeartbeatTimeout(0);
        aioClient = new AioClient(clientGroupContext);
        clientChannelContext = aioClient.connect(serverNode);
        //连上后，发条消息玩玩
        send();
        new SpringApplicationBuilder(AppBootstrap.class).run(args);
    }

    private static void send() throws Exception {
        byte[] loginBody = new LoginReqBody("hello_clientss","12344").toByte();
        TcpPacket loginPacket = new TcpPacket(Command.COMMAND_LOGIN_REQ,loginBody);
        //先登录
        Aio.send(clientChannelContext, loginPacket);
        ChatBody chatBody = new ChatBody()
            .setFrom("hello_clientss")
            .setTo("1574299945153")
            .setMsgType(0)
            .setChatType(1)
            .setGroup_id("100")
            .setContent("Socket普通客户端消息测试!");
        TcpPacket chatPacket = new TcpPacket(Command.COMMAND_CHAT_REQ,chatBody.toByte());
        Aio.send(clientChannelContext, chatPacket);
    }
}
